package com.neuedu.cockpit.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neuedu.cockpit.domain.HmCarInfo;
import com.neuedu.cockpit.domain.HmCarState;
import com.neuedu.cockpit.domain.HmControlLog;
import com.neuedu.cockpit.domain.HmDoorLightState;
import com.neuedu.cockpit.service.IHmCarInfoService;
import com.neuedu.cockpit.service.IHmCarStateService;
import com.neuedu.cockpit.service.IHmControlLogService;
import com.neuedu.common.annotation.Log;
import com.neuedu.common.constant.Command;
import com.neuedu.common.core.controller.BaseController;
import com.neuedu.common.core.domain.AjaxResult;
import com.neuedu.common.domain.po.CommandPo;
//import com.neuedu.common.domain.po.MediaCommandPo;
import com.neuedu.common.enums.BusinessType;

import com.neuedu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 车辆控制Controller
 *
 * @author neuedu
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/cockpit/control")
public class HmCarControlController extends BaseController {
    @Autowired
    private IHmCarStateService hmCarStateService;

    @Autowired
    private IHmCarInfoService hmCarInfoService;

    @Autowired
    private IHmControlLogService iHmControlLogService;



    // MQTT配置已移除

    /**
     * 创建车辆移动日志
     *
     * @param carId
     */
    private int createLog(Long carId) {
        HmCarInfo hmCarInfo = hmCarInfoService.selectHmCarInfoById(carId);
        HmControlLog hmControlLog = new HmControlLog();
        hmControlLog.setCarId(carId);
        hmControlLog.setUserId(getUserId());
        hmControlLog.setCreateTime(new Date());
        hmControlLog.setOperaType("车辆移动");
        hmControlLog.setOperaContent("用户" + getUsername() + "控制车辆" + hmCarInfo.getCarName() + "进行了车辆移动操作");
        return iHmControlLogService.insertHmControlLog(hmControlLog);
    }

    /**
     * 车辆前进
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/move/{carId}")
    public AjaxResult move(@PathVariable Long carId) {
        // 发送MQTT - 已移除
        return toAjax(createLog(carId));
    }

    /**
     * 车辆后退
     *
     * @param carId
     * @return
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/back/{carId}")
    public AjaxResult back(@PathVariable Long carId) {
        // 发送MQTT - 已移除
        return toAjax(createLog(carId));
    }

    /**
     * 车辆左转
     *
     * @param carId
     * @return
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/left/{carId}")
    public AjaxResult left(@PathVariable Long carId) {
        // 发送MQTT - 已移除
        return toAjax(createLog(carId));
    }

    /**
     * 车辆右转
     *
     * @param carId
     * @return
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/right/{carId}")
    public AjaxResult right(@PathVariable Long carId) {
        // 发送MQTT - 已移除
        return toAjax(createLog(carId));
    }

    /**
     * 车辆停车
     *
     * @param carId
     * @return
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/stop/{carId}")
    public AjaxResult stop(@PathVariable Long carId) {
        HmCarInfo hmCarInfo = hmCarInfoService.selectHmCarInfoById(carId);
        HmControlLog hmControlLog = new HmControlLog();
        hmControlLog.setCarId(carId);
        hmControlLog.setUserId(getUserId());
        hmControlLog.setCreateTime(new Date());
        hmControlLog.setOperaType("车辆移动");
        hmControlLog.setOperaContent("用户" + getUsername() + "控制车辆" + hmCarInfo.getCarName() + "进行了车辆停止操作");
        // 发送MQTT - 已移除
        return toAjax(iHmControlLogService.insertHmControlLog(hmControlLog));
    }

    /**
     * 车辆门控制
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/door")
    public AjaxResult door(@RequestBody HmDoorLightState hmDoorLightState) {
        CommandPo commandPo = new CommandPo();
        HmCarState hmCarState = new HmCarState();
        hmCarState.setCarId(hmDoorLightState.getCarId());
        hmCarState.setDoor(hmDoorLightState.getDoor());
        hmCarState.setCarVin(hmDoorLightState.getCarVin());
        HmCarState nowCarState = hmCarStateService.selectHmCarStateByCarId(hmDoorLightState.getCarId());
        if (StringUtils.equals(hmDoorLightState.getDoor(), "1") && StringUtils.equals(nowCarState.getDoor(), "1")) {
            return AjaxResult.error("当前车辆门已开");
        }
        int result = hmCarStateService.updateHmCarState(hmCarState);
        commandPo.setCarId(hmDoorLightState.getCarId());
        if (StringUtils.equals(hmDoorLightState.getDoor(), "1")) {
            commandPo.setCommand(Command.OPENDOOR);
        } else {
            commandPo.setCommand(Command.CLOSEDOOR);
        }
        if (result == -1) {
            return AjaxResult.error("当前用户未绑定车辆");
        } else {
            // 给车辆控制发MQTT - 已移除
            return toAjax(result);
        }
    }

    /**
     * 车辆车灯控制
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/light")
    public AjaxResult light(@RequestBody HmDoorLightState hmDoorLightState) {
        CommandPo commandPo = new CommandPo();
        HmCarState hmCarState = new HmCarState();
        hmCarState.setCarId(hmDoorLightState.getCarId());
        hmCarState.setLight(hmDoorLightState.getLight());
        hmCarState.setCarVin(hmDoorLightState.getCarVin());
        int result = hmCarStateService.updateHmCarState(hmCarState);
        commandPo.setCarId(hmDoorLightState.getCarId());
        if (StringUtils.equals(hmDoorLightState.getLight(), "1")) {
            commandPo.setCommand(Command.OPENLIGHT);
        } else {
            commandPo.setCommand(Command.CLOSELIGHT);
        }
        if (result == -1) {
            return AjaxResult.error("当前用户未绑定车辆");
        } else {
            // MQTT发送已移除
            return toAjax(result);
        }
    }

    /**
     * 车辆NFC识别
     *
     * @param nfc
     * @return
     */
    @Log(title = "车辆状态", businessType = BusinessType.UPDATE)
    @PostMapping("/nfc/{nfc}")
    public AjaxResult nfc(@PathVariable String nfc) {
        LambdaQueryWrapper<HmCarInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HmCarInfo::getNfc, nfc);
        queryWrapper.eq(HmCarInfo::getUserId, getUserId());
        List<HmCarInfo> list = hmCarInfoService.list(queryWrapper);
        if (list.isEmpty()) {
            return AjaxResult.error("未找到车辆或没有操控权限");
        } else {
            // 开车门
            HmCarInfo hmCarInfo = list.get(0);
            HmDoorLightState hmDoorLightState = new HmDoorLightState();
            hmDoorLightState.setDoor("1");
            hmDoorLightState.setCarId(hmCarInfo.getId());
            hmDoorLightState.setCarVin(hmCarInfo.getCarVin());
            return door(hmDoorLightState);
        }
    }

    public CommandPo getCommandPo(Long carId, String command) {
        CommandPo commandPo = new CommandPo();
        commandPo.setCarId(carId);
        commandPo.setCommand(command);
        return commandPo;
    }

    /**
     * 媒体控制
     */
//    @Log(title = "OpenHarmony 媒体控制", businessType = BusinessType.UPDATE)
//    @GetMapping("/media")
//    public  AjaxResult media(Long carId, String command, String mediaType) {
//        try {
//            MediaCommandPo mediaCommandPo = new MediaCommandPo();
//            mediaCommandPo.setCarId(carId);
//            mediaCommandPo.setMediaType(mediaType);
//            mediaCommandPo.setCommand(command);
//            mqttGateway.sendToMqtt(mediaTopic, 2, JSON.toJSONString(mediaCommandPo));
//        }catch (Exception e){
//            return AjaxResult.error("媒体控制失败");
//        }
//        return AjaxResult.success();
//    }
}
